<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mod_article extends CI_Controller {
    public $setting = '';
    private $allchild = array();
    private $per_page = 10;
    private $total_rows = 6;
    function  __construct(){
        parent::__construct();
        $this->setting =$this->common_model->getone('setting',array('id'=>1));
        
    }
    function index($param1,$param2=''){
        if(empty($param2) || is_numeric($param2)){
            if($param1 == 'gioi-thieu' || $param1 == 'about-us')
                $this->aboutus();
            else
                $this->category();
        }else{
            $this->detail();
        }
    }
    function tinnoibat(){
        $this->load->view('mod_article/noibat',
                        array(
                            'data'=>$this->common_model->getdata('article',array('publish'=>1),array(),array('id','DESC'),5))
                         );     
    }
    function catecreat($arrcate=array(),$id = 0){
        if(isset($arrcate[$id])){
            foreach ($arrcate[$id] as $onecate ){
                $this->allchild[] = $onecate->id;
                if(!empty($arrcate[$onecate->id])){
                    $this->catecreat($arrcate,$onecate->id);
                }
            }
            return $this->allchild;
        }else{
            return array();
        }
    }
    function category($alias){
        //lay danh muc
        $category_alias = preg_replace("/(.0)/", '', $alias);  
        $category = $this->common_model->getone('article_category',array('alias'=>$category_alias,'publish'=>1));
        if(!empty($category)){
            //co danh muc   
            $new_article = $this->common_model->getdata('article',array('publish'=>1,'category'=>$category->id),array(),array('id','DESC'),5);         
            $offset = $this->uri->segment(2,0);
            $article = $this->common_model->getdata('article',array('category'=>$category->id),array('title','alias','created','introtext','image'),'',array($this->per_page,$offset));
            $this->total_rows = $this->common_model->getnumber('article',array('category',$category->id));
            $this->phantrang();
            $data['content'] = $this->load->view('mod_article/category',array('article'=>$article,'category'=>$category,'new_article'=>$new_article),true);
            $data['title'] = $category->title;
            $data['metades'] = $category->metades;
            $data['metakey'] = $category->metakey;            
        }else{
            $data['title'] = $this->setting->title;
            $data['metades'] = $this->setting->metades;
            $data['metakey'] = $this->setting->metakey;
            $data['content']='Nội dụng không tồn tại!';
        }        
        $this->load->view('default/index',$data); 
    }
    function detail($alias){
        $alias = preg_replace("/(.6)/", '', $alias);  
        $article = $this->common_model->getone('article',array('alias'=>$alias,'publish'=>1));
        if(!empty($article)){
            $category = $this->common_model->getone('article_category',array('id'=>$article->category,'publish'=>1),array('id','title'));
            $data['content'] = $this->load->view('mod_article/detail',array('article'=>$article,'catagory'=>$category),true);
            $data['title'] = $article->title;
            $data['metades'] = $article->metades;
            $data['metakey'] = $article->metakey; 
        }else{
            $data['title'] = $this->setting->title;
            $data['metades'] = $this->setting->metades;
            $data['metakey'] = $this->setting->metakey;
        }        
       
        $this->load->view('default/index',$data);         
    }
    function aboutus(){        
        $about = $this->common_model->getone('about',array('id'=>1));        
        if(!empty($about)){
            $data['content'] = $this->load->view('mod_article/about',array('about'=>$about),true);
            
             
        }  
        $data['title'] = 'Giới thiệu';
            $data['metades'] = $this->setting->metades;
            $data['metakey'] = $this->setting->metakey;     
        $data['breadcrumbs'] = '<span>Giới thiệu</span>';                     
        $this->load->view('default/index',$data); 
    }
    function phantrang(){
        $this->load->library('pagination');
        $config['base_url'] = base_url().$this->uri->segment(1).'/';
        $config['total_rows'] = $this->total_rows;
        $config['per_page'] = $this->per_page;
        $config['uri_segment'] = 2;
        $config['num_links'] = 12;
        $this->pagination->initialize($config);
    }
}